package com.guyj.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.guyj.baseapp.R;
import com.guyj.common.http.HttpUtils;
import com.guyj.common.utils.LogUtils;
import com.guyj.common.views.CustomProgress;
import com.guyj.common.views.LazyTextView;

import butterknife.ButterKnife;

/**
 * Activity基类
 */
public abstract class BaseActivity extends Activity {

    private CustomProgress pd;
    protected Context context = this;
    private boolean cancelable = true;
    protected String TAG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        TAG = this.getClass().getSimpleName();
        LogUtils.d(TAG, "-isCreate");
        ButterKnife.bind(this);
        initView(savedInstanceState);
        initVariable();
        processLogic(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.d(TAG, "-isDestroy");
        ButterKnife.unbind(this);
    }

    public abstract int getContentViewId();

    protected void setBack() {
        ImageView back = (ImageView) findViewById(getResources().getIdentifier("back","id",context.getPackageName()));
        if (back == null)
            return;
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    protected void setTitle(String titleName) {
        TextView title = (TextView) findViewById(getResources().getIdentifier("title","id",context.getPackageName()));
        if (title == null)
            return;
        title.setText(titleName);
    }

    protected void setRight() {
        LazyTextView right = (LazyTextView) findViewById(getResources().getIdentifier("submit","id",context.getPackageName()));
        if (right == null)
            return;
        right.setVisibility(View.VISIBLE);
    }

    /**
     * 初始化布局以及View控件
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 初始化变量
     */
    protected abstract void initVariable();

    /**
     * 给View控件添加事件监听器
     */
//    protected abstract void setListener();

    /**
     * 处理业务逻辑，状态恢复等操作
     */
    protected abstract void processLogic(Bundle savedInstanceState);

    /**
     * 调用接口时，显示dialog
     */
    public void initProgressDialog() {
        if (pd == null) {
            pd = CustomProgress.show(context, context.getResources().getString(R.string.loading),
                    cancelable, cancelable ? new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            HttpUtils.cancelPost();
                        }
                    } : null);

            if (!pd.isShowing()) {
                pd.show();
            }
        }
    }

    /**
     * 处理接口返回时，隐藏dialog
     */
    public void dismissProgressDialog() {
        if (pd != null) {
            pd.dismiss();
            pd = null;
        }
    }
}
